package persistenceObjectHandler;

//import com.sun.org.apache.bcel.internal.generic.TABLESWITCH;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;


/**
 *
 * @author jano
 * @version 1.0
 *
 */
public class HandlerObjectPersistence {

    /**
     * metodo que carga un objeto de la base de datos segun su id, y su
     *      tipo de clase
     * @param idObject id del objecto a cargar
     * @param objectClass Clase del objecto, siempre es Objecto.class
     * @return object con valor nulo si falla, o el objeto que tenga el id
     *      correspondiente a idObject
     */
    public static Object loadObject(int idObject, Class objectClass){
        Transaction transaction=null;
        Object object = null;
        try
        {
            Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
            transaction=session.beginTransaction();
            object = (Object) session.get(objectClass, new Integer(idObject));
            transaction.commit();
        }
        catch(HibernateException exception)
        {
            System.out.println(exception.toString());
            transaction.rollback();
        }
        return object;
    }

    public static Object loadObjectByLongId(long idObject, Class objectClass){
        Transaction transaction=null;
        Object object = null;
        try
        {
            Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
            transaction=session.beginTransaction();
            object = (Object) session.get(objectClass, new Long(idObject));
            transaction.commit();
        }
        catch(HibernateException exception)
        {
            System.out.println(exception.toString());
            transaction.rollback();
        }
        
        return object;
    }



    
    /**
     * metodo que guarda un objeto en la base de datos, si el objeto no existe
     *      se crea una nueva tupla que representa al objeto, en caso contrario sí 
     *      existe este se actualiza
     * @param object el objeto a guardar o actualizar según corresponda
     * @return resultado de la operación 1 exito, 0 fracaso
     */
    public static boolean saveObject(Object object){
        Transaction transaction = null;
        Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
        try
        {
            session.clear();
            session.save(object);
            transaction.commit();
            return true;
        }
        catch(HibernateException exception){
            System.out.println(exception.toString());
            transaction.rollback();
            return false;
        }
        
    }
    public static boolean updateObject(Object object){
        Transaction transaction = null;
        Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
        try
        {
            session.clear();
            session.update(object);
            transaction.commit();
            
            return true;
        }
        catch(HibernateException exception){
            System.out.println(exception.toString());
            transaction.rollback();
            return false;
        }
    }

    public static boolean saveOrUpdateObject(Object object){
        Transaction transaction = null;
        Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
        try
        {
            session.clear();
            session.update(object);
            transaction.commit();
            HibernateSessionManager.getSessionFactory().close();
            return true;
        }
        catch(HibernateException exception){
            System.out.println(exception.toString());
            transaction.rollback();
            return false;
        }
    }
    /**
     * metodo que carga todos los objetos de una Clase de la base de datos
     * @param objectClass la Clase del objeto, se obtiene como Objeto.class
     * @return lista de los objetos de la Clase consultada
     */
    public static List getAllObjectByClassType(Class objectClass){
        Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
        Transaction transaction = null;
        transaction =session.beginTransaction();
        List list = null;
        try
        {
            Criteria  criteria =  session.createCriteria(objectClass);
            list = criteria.list();
            transaction.commit();
        }
        catch(HibernateException exception)
        {
            System.out.println(exception.toString());
            transaction.rollback();

        }
        return list;
    }


    /**
     * metodo que purga un objeto de manera permanente de la base de datos
     * @param object objeto a eliminar
     * @return resultado de la operación 1 exito, 2 fracaso
     */
    public static boolean PurgeObject(Object object){
        Transaction transaction = null;
        Session session = HibernateSessionManager.getSessionFactory().getCurrentSession();
        transaction = session.beginTransaction();
        try
        {
            session.delete(object);
            transaction.commit();
            return true;
        }
        catch(HibernateException exception)
        {
            System.out.println(exception.toString());
            transaction.rollback();
            return false;
        }

    }
    
}
